//
//  BRLanguageManager.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/11.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRLanguageManager.h"
#import "AppDelegate.h"

@implementation BRLanguageManager

+ (instancetype)share {
    
    static BRLanguageManager *man = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        man = [[BRLanguageManager alloc] init];
    });
    
    return man;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setDefaultLanguage];
    }
    return self;
}


- (void)request:(NSString *)la {
    // 接口已废弃，注释掉
    /*
    [[BRRequestManager shared] PUT:[BRAPI languageSwitch:la] params:nil succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        
    }];
    */
}

- (void)setDefaultLanguage {
    if (![[NSUserDefaults standardUserDefaults] objectForKey:kBRLanguageKey]) {
        // 默认语言设置为越南语
        [[NSUserDefaults standardUserDefaults] setObject:@"vi" forKey:kBRLanguageKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (NSString *)stringWithLanguageKey:(NSString *)key table:(NSString *)table {

    NSString *defaultKey = [[NSUserDefaults standardUserDefaults] objectForKey:kBRLanguageKey];
    NSString *patch = [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",defaultKey] ofType:@"lproj"]] localizedStringForKey:(key) value:nil table:table];
    return patch;
}

- (NSString *)stringWithLanguageKey:(NSString *)key {
    return [self stringWithLanguageKey:key table:@"Language"];
}

- (void)setNewLanguage:(NSString *)language changeNew:(BOOL)newWindow
{
    if ([language isEqualToString:self.language])
    {
        return;
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:language forKey:kBRLanguageKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    if (newWindow) {
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [app didInitWindow];
    }
}

- (void)setNewLanguage:(NSString *)language
{
    
    [self setNewLanguage:language changeNew:YES];
    
}
- (NSString *)language {
    return [[NSUserDefaults standardUserDefaults] objectForKey:kBRLanguageKey];
}

- (NSString *)describe {
    
    for (BRLanguageModel *model in self.languages) {
//        NSLog(@"%@",model.describe);
        if ([model.language isEqualToString:self.language]) {
            return model.describe;
        }
    }
    
    return @"Tiếng Việt";
}

- (BOOL)isChinese {
    
    if ([self.language hasPrefix:@"zh-Hans"]){
        return YES;
    }
    return NO;
}

- (NSArray *)languageDescribes {
    
    if (_languageDescribes == nil) {
        NSMutableArray *las  = [NSMutableArray array];
        for (BRLanguageModel *model in self.languages) {
            [las addObject:model.describe];
        }
        return las;
    }
    return _languageDescribes;
}

- (NSArray <BRLanguageModel *>*)languages {
    //br_requstLanguage 新增语言修改这
    if (_languages == nil) {
        _languages = [BRLanguageModel mj_objectArrayWithKeyValuesArray:@[
            @{
                @"language" : @"vi",
                @"languageRequestStr" : @"VI",
                @"describe" : @"Tiếng Việt",
            },
            @{
                @"language" : @"zh-Hans",
                @"languageRequestStr" : @"ZH",
                @"describe" : @"简体中文",
            },
            @{
                @"language" : @"en",
                @"languageRequestStr" : @"EN",
                @"describe" : @"English",
            },
        ]];
    }
    return _languages;
}

- (NSString *)requestStr {
    
    for (BRLanguageModel *model in self.languages) {
//        NSLog(@"%@",model.describe);
        if ([model.language isEqualToString:self.language]) {
            return model.languageRequestStr;
        }
    }
    
    return @"VI";
}

#pragma mark - tool --
+ (NSString *)br_languageKey:(NSString *)key inArr:(NSArray *)arr {
    
    if ([arr containsObject:key]) {
        return key;
    }
    return @"vi";
}

@end

@implementation BRLanguageModel


@end

@implementation BRLanguageKeyModel


@end
